package com.platform.netty.udp.task;

import com.platform.common.manager.AsyncManager;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.spring.SpringUtils;
import com.platform.module.domain.TempRecord;
import com.platform.module.service.ITempRecordService;
import com.platform.netty.tcp.task.AlarmPoolTask;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.TimerTask;

@Data
@Slf4j
public class TempPoolTask {

    private static ITempRecordService tempRecordService;

    /**
     * 室温采集器数保存数据库
     *
     * @param datas 室温采集器接收到的数据
     * @return 任务task
     */
    public static TimerTask saveTemp(final String datas)
    {
        tempRecordService = SpringUtils.getBean(ITempRecordService.class);

        return new TimerTask()
        {
            @Override
            public void run()
            {
                //将消息内容拆分称数组
                String[] contents = datas.split(" ");
                //设备id
                String tempId = contents[23]+contents[24]+contents[25]+contents[26]+contents[27]+contents[28]+contents[29]+
                        contents[30]+contents[31]+contents[32]+contents[33]+contents[34]+contents[35]+contents[36]+contents[37];
                tempId = NettyUtil.hexStr2Str(tempId);
                try {
                    //室内温度
                    String temp = NettyUtil.get10HexNum(contents[64]) + "." + NettyUtil.get10HexNum(contents[65]);
                    //室内湿度
                    String damp = NettyUtil.get10HexNum(contents[66]).toString();
                    //电池电量
                    String elec = NettyUtil.get10HexNum(contents[67]) + "." + NettyUtil.get10HexNum(contents[68]);
                    //内部NTC故障报警
                    String alarm1 = contents[72];
                    //外部NTC故障报警
                    String alarm2 = contents[73];
                    //温湿度传感器故障报警
                    String alarm3 = contents[74];

                    if(!alarm1.equals("00") && !alarm2.equals("00") && !alarm3.equals("00")){
                        String alarmMsg = "";
                        if(alarm1.equals("01")){
                            alarmMsg = "内部NTC故障报警";
                        }else if(alarm2.equals("01")){
                            alarmMsg = "外部NTC故障报警";
                        }else if(alarm3.equals("01")){
                            alarmMsg = "温湿度传感器故障报警";
                        }
                        AsyncManager.me().execute(AlarmPoolTask.saveAlarm(tempId, "4", "1", "室温采集", alarmMsg));
                    }

                    if(Double.parseDouble(temp)==0){
                        AsyncManager.me().execute(AlarmPoolTask.saveAlarm(tempId, "4", "1", "室温采集", "温度采集不正确"));
                    }else{
                        TempRecord record = new TempRecord();
                        record.setTempId(tempId);
                        record.setTemp(temp);
                        record.setDamp(damp);
                        record.setElec(elec);
                        record.setCreateTime(new Date());
                        record.setAlarm1(alarm1);
                        record.setAlarm2(alarm2);
                        record.setAlarm3(alarm3);
                        tempRecordService.insertTempRecord(record);
                    }
                }catch (Exception e){
                    e.printStackTrace();

                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(tempId, "4", "1", "室温采集", "室温采集器"+tempId+"数据["+datas+"]有误，解析错误"+e.getMessage()));
                }
            }
        };
    }
}
